import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;

class Node{
    int x;
    int y;
    public Node(int x,int y){
        this.x=x;
        this.y=y;
    }
}
public class TestDemo10B {
    public static String getMaxSub(String str1, String str2) {
        char[] array1 = str1.toCharArray();
        char[] array2 = str2.toCharArray();
        int len1 = array1.length;
        int len2 = array2.length;
        //最长子串的长度
        int max = 0;
        //最长子串的起始位置
        int start = 0;
        //记录该位置下字符串的长度
        int[][] maxLength = new int[len1 + 1][len2 + 1];
        for (int i = 1; i <= len1; i++) {
            for (int j = 1; j <= len2; j++) {
                //如果第i个字符和第j个字符相等,则进行累加
                if (array1[i - 1] == array2[j - 1]) {
                    //这里记录的是i-1和j-1之前相等字符串长度再加上i-1和j-1
                    //当前相等的情况(+1)
                    maxLength[i][j] = maxLength[i - 1][j - 1] + 1;
                }//记录最长公共字符串
                if (max < maxLength[i][j]) {
                    max = maxLength[i][j];
                    start = i - max;//代表着公共字符串的起始位置
                }
            }
        }
        return str1.substring(start, start + max);
    }

    public static void main1(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String str1 = reader.readLine();
        String str2 = reader.readLine();
        //从最小字符串中取公共字符串
        if (str1.length() < str2.length()) {
            System.out.println(getMaxSub(str1, str2));
        } else {
            System.out.println(getMaxSub(str2, str1));
        }
    }

    //洗牌
    public static void main2(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            int m = sc.nextInt();
            int k = sc.nextInt();
            int[] ch = new int[2 * m];
            for (int j = 0; j < ch.length; j++) {
                ch[j] = sc.nextInt();
            }
            funcA(ch, m, k);
        }
    }

    public static void funcA(int[] ch, int m, int k) {
        for (int i = 0; i < k; i++) {
            //存放洗牌一次的牌的顺序
            int[] cards = new int[ch.length];
            for (int j = 0; j < m; j++) {
                cards[2 * j] = ch[j];
                cards[2 * j + 1] = ch[j + m];
            }
            //因为cards定义在for循环里面,不能出for外面,但ch可以
            ch = cards;
        }
        funcB(ch);
    }

    //打印函数
    public static void funcB(int[] cards) {
        for (int i = 0; i < cards.length - 1; i++) {
            System.out.print(cards[i] + " ");
        }
        System.out.println(cards[cards.length - 1]);
    }

    //MP3光标位置
    public static void main3(String[] args) throws Exception {
        //换行输入的写法
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        String str = reader.readLine();
        char[] ch = str.toCharArray();
        int mouse = 1;//光标
        int first = 1;//该页面的首位置
        mouseMove(n, ch, mouse, first);
    }

    public static void mouseMove(int n, char[] ch, int mouse, int first) {
        if (n <= 4) {
            //n小于4的时候,不需要移到页面,first固定了
            for (int i = 0; i < ch.length; i++) {
                //光标在1位置上时,往上移直接跳到最后一个位置上
                if (mouse == 1 && ch[i] == 'U') {
                    mouse = n;
                }//光标在最后一个位置时,往下移直接跳动第一个位置上
                else if (mouse == n && ch[i] == 'D') {
                    mouse = 1;
                } else if (ch[i] == 'U') {
                    mouse--;
                } else if (ch[i] == 'D') {
                    mouse++;
                }
            }//打印
            for (int j = 1; j < n; j++) {
                System.out.print(j + " ");
            }
            System.out.println(n);
            System.out.println(mouse);
        } else {//当n>4的时候
            for (int i = 0; i < ch.length; i++) {
                //在first和mouse在1往下移直接移到到最后一个光标
                if (first == 1 && mouse == 1 && ch[i] == 'U') {
                    first = n - 3;//first也会改变,该页面的第一个值
                    mouse = n;
                }//同理在尾部的时候
                else if (first == n - 3 && mouse == n && ch[i] == 'D') {
                    first = 1;
                    mouse = 1;
                }//mouse在该页面第一个位置时,往上移到,first和mouse都--
                else if (first != 1 && mouse == first && ch[i] == 'U') {
                    mouse--;
                    first--;
                }//mouse在该页面最后一个位置时,往下移到,mouse和first都++
                else if (first != n - 3 && mouse == first + 3 && ch[i] == 'D') {
                    mouse++;
                    first++;
                } else if (ch[i] == 'U') {
                    mouse--;
                } else if (ch[i] == 'D') {
                    mouse++;
                }
            }
            for (int j = first; j < first + 3; j++) {
                System.out.print(j + " ");
            }
            System.out.println(first + 3);
            System.out.println(mouse);
        }
    }

    //找出字符串中第一个只出现一次的字符  HJ59
    public static void main4(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        char[] ch = str.toCharArray();
        //存的是ch字符的ASCILL的值
        int[] count = new int[128];
        for (int i = 0; i < ch.length; i++) {
            //该字符的ascill出现一次++；
            count[ch[i]]++;
        }
        //考虑不满足的情况
        boolean flag = true;
        for (int i = 0; i < ch.length; i++) {
            //如果count为1，代表第一次出现并且只出现一次。
            if (count[ch[i]] == 1) {
                System.out.println(ch[i]);
                flag = false;
                break;
            }
        }
        if (flag) {
            System.out.println(-1);
        }
    }

    //年终奖
    public int getMost(int[][] board) {
        // write code here
        int len1 = board.length;
        int len2 = board[0].length;
        //加这两次for循环的目的是因为最后两次for循环不经过第一行和第一列
        //这两次for循环记录了第一行和第一列的值
        for (int i = 1; i < len1; i++) {
            board[i][0] += board[i - 1][0];
        }
        for (int j = 1; j < len2; j++) {
            board[0][j] += board[0][j - 1];
        }
        for (int i = 1; i < len1; i++) {
            for (int j = 1; j < len2; j++) {
                board[i][j] = Math.max(board[i - 1][j], board[i][j - 1]) + board[i][j];
            }
        }
        return board[len1 - 1][len2 - 1];
    }

//迷宫问题
    public static void getMinPath(int [][]mat,int row,int col,int x,int y,int[][]book,ArrayList<Node>path,ArrayList<Node>minPath){
        //判断x,y是否越界,是否经过一次,是否有障碍(mat[x][y]==1)
        if(x<0||y<0||x>=row||y>=col||book[x][y]==1||mat[x][y]==1){
            return ;
        }//符合条件就存入路径
        path.add(new  Node(x,y));
        //book来判断是否经过一次
        book[x][y]=1;
        //判断是否为出口
        if(x==row-1&&y==col-1){
            //如果该路径比短路(minPath)径还小,就把原先短路径(minPath)清空掉,然后添加新路径
            //这个minPath.isEmpty的目的是因为第一次的时候短路径是空的,如果不加,就永远不满足if内条件
            //第一次添加路径就往最短路径内添加
            if(minPath.isEmpty()||path.size()<minPath.size()){
                minPath.clear();
                for(Node n:path){
                    minPath.add(n);
                }

            }
        }
        //往x,y四个方向遍历
        getMinPath(mat,row,col,x+1,y,book,path,minPath);
        getMinPath(mat,row,col,x-1,y,book,path,minPath);
        getMinPath(mat,row,col,x,y+1,book,path,minPath);
        getMinPath(mat,row,col,x,y-1,book,path,minPath);
        //回溯.寻找新的路径.
        path.remove(path.size()-1);
    }
    public static void main(String[]args){
        Scanner sc=new Scanner(System.in);
        int row=sc.nextInt();
        int col=sc.nextInt();
        int[][]mat=new int[row][col];
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                mat[i][j]=sc.nextInt();
            }
        }
        ArrayList<Node>path=new ArrayList<>();
        ArrayList<Node>minPath=new ArrayList<>();
        int [][]book=new int[row][col];
        getMinPath(mat,row,col,0,0,book,path,minPath);
        for(Node n:minPath){
            System.out.println("("+n.x+","+n.y+")");
        }
    }
}